<?php
namespace app\admin\service;

use app\common\model\ChargingModel;

class ChargingService
{
    private static $chargingModel;

    public function __construct(ChargingModel $chargingModel)
    {
        self::$chargingModel = $chargingModel;
    }

    /**
     * 显示充电桩列表
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午2:11
     * @param $param
     * @return array
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($param)
    {
        if (empty($param) || !isset($param['page']) || !isset($param['limit'])) {
            app_fail(9997); // 参数异常
        }

        $where = [['is_del', '=', 1]];

        if (!empty($param['charge_type'])) {
            $where = array_merge($where, [['charge_type', '=', $param['charge_type']]]);
        }

        if (!empty($param['charge_name'])) {
            $where = array_merge([['charge_name', 'like', "%".$param['charge_name']."%"]]);
        }
        // 获取总数
        $count = self::$chargingModel->where($where)->count();
        if ($count < 1) {
            return ['data' => [], 'count' => $count];
        }
        // 查询管理员列表
        $result = self::$chargingModel->where($where)
            ->page($param['page'])
            ->limit($param['limit'])
            ->order('create_time', 'desc')
            ->select();
        return ['data' => $result, 'count' => $count];
    }

    /**
     * 添加充电桩类型
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:46
     * @param $param
     * @return bool
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function store($param)
    {
        if (empty($param) || !isset($param['charge_num'])) {
            app_fail(9997);
        }

        // 检查充电桩是否存在
        $row = self::$chargingModel->where('charge_num', '=', $param['charge_num'])->find();
        if (!empty($row)) {
            app_fail(9994);
        }

        try {
            self::$chargingModel->allowField(true)->save($param);
            return true;
        } catch (\Exception $e) {
            app_fail(9998);
        }
    }

    /**
     * 显示指定充电桩数据
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:54
     * @param $param
     * @return array|bool|null|\PDOStatement|string|\think\Model
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($param)
    {
        if (empty($param) || !isset($param['id'])) {
            app_fail(9997);
        }

        $row = self::$chargingModel->where('id', '=', $param['id'])->find();
        return $row ? $row : false;
    }

    /**
     * 修改充电桩类型
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:58
     * @param $param
     * @return bool
     * @throws \app\common\exception\AppException
     */
    public function update($param)
    {
        if (empty($param) || !isset($param['id'])) {
            app_fail( 9997);
        }

        try {
            $id = $param['id'];
            unset($param['id']);
            self::$chargingModel->allowField(true)->save($param, ['id' => $id]);
            return true;
        } catch (\Exception $e) {
            app_fail(9998);
        }

    }
}